package service

import (
	"encoding/json"
	"fmt"
	"reward-server/forms/request"
	"reward-server/forms/response"
	"reward-server/global"
	mq "reward-server/models/mysql"

	r "github.com/go-resty/resty/v2"
)

type IFateService interface {
	Create(req request.FateReq) (err error)
	GetList(req request.FateSearchReq) (res *response.ListResp, err error)
}

type FateService struct {
	Fate mq.IFates
}

func NewFatesService() IFateService {

	return &FateService{
		Fate: mq.NewFates(),
	}
}

type DeepSeek struct {
	Model    string    `json:"model"`
	Messages []Message `json:"messages"`
	Stream   bool      `json:"stream"`
}

type DeepSeekRes struct {
	ID                string    `json:"id"`
	Object            string    `json:"object"`
	Created           int64     `json:"created"`
	Model             string    `json:"model"`
	Choices           []Choices `json:"choices"`
	Usage             Usage     `json:"usage"`
	SystemFingerprint string    `json:"system_fingerprint"`
}

type Choices struct {
	Index        int     `json:"index"`
	Message      Message `json:"message"`
	Logprobs     any     `json:"logprobs"`
	FinishReason string  `json:"finish_reason"`
}
type Usage struct {
	PromptTokens        int `json:"prompt_tokens"`
	CompletionTokens    int `json:"completion_tokens"`
	TotalTokens         int `json:"total_tokens"`
	PromptTokensDetails struct {
		CachedTokens int `json:"cached_tokens"`
	} `json:"prompt_tokens_details"`
	PromptCacheHitTokens  int `json:"prompt_cache_hit_tokens"`
	PromptCacheMissTokens int `json:"prompt_cache_miss_tokens"`
}
type Message struct {
	Role    string `json:"role"`
	Content string `json:"content"`
}

func (s *FateService) Create(req request.FateReq) (err error) {

	//拼装deepseek请求数据
	
	data := DeepSeek{
		Model:    "deepseek-chat",
		Messages: []Message{{Role: "user", Content: "你好"}},
		Stream:   false,
	}

	resp, err := r.New().SetDebug(true).
		SetHeader("Content-Type", "application/json").
		SetHeader("Authorization", fmt.Sprintf("Bearer %s", global.ServerConfig.DeepSeek.Key)).
		SetBody(data).
		Post(global.ServerConfig.DeepSeek.Url)
	var res DeepSeekRes
	err = json.Unmarshal(res.Body(), &res)
	if err != nil {
		return err
	}
	return res.Choices[0].Message.Content, nil
	// err = s.Wall.Insert(&mq.Fates{
	// 	Name:        req.Name,
	// 	AccountAddr: req.AccountAddr,
	// 	Description: req.Description,
	// })

	return
}

func (s *FateService) GetList(req request.FateSearchReq) (res *response.ListResp, err error) {

	// result, total, err := s.Fate.GetList(req)
	// if err != nil {
	// 	return nil, err
	// }
	// var data []*response.WishWall
	// for _, v := range result {
	// 	data = append(data, &response.WishWall{
	// 		Id:          v.Id,
	// 		Name:        v.Name,
	// 		AccountAddr: v.AccountAddr,
	// 		Description: v.Description,
	// 	})
	// }

	// return &response.ListResp{
	// 	Total: total,
	// 	Data:  data,
	// }, nil
	return
}
